#include "CPlugExtendDlg.h"
#include <qdir.h>
#include <qpluginloader.h>
#include <qfont.h>
#include <qicon.h>

#include "ui_CPlugExtendDlg.h"
#include "CDefInterfaces.h"
namespace Ui {
	class CPlugExtendDlgClass;
}
Ui::CPlugExtendDlgClass ui;

#pragma execution_character_set("utf-8")


class CPlugExtendDlgPrivate
{
public:
	explicit CPlugExtendDlgPrivate(CPlugExtendDlg* parent);
	~CPlugExtendDlgPrivate();
private:
	CPlugExtendDlg* const q_ptr;
	QString m_plug_path;
	QStringList m_filenames;
	QIcon m_interface_icon;
	QIcon m_feature_icon;
	Q_DECLARE_PUBLIC(CPlugExtendDlg)
};


CPlugExtendDlg::CPlugExtendDlg(QWidget *parent)
	: QDialog(parent), d_ptr{new CPlugExtendDlgPrivate(this)}
{
	ui.setupUi(this);
	Q_ASSERT(d_ptr);
}

CPlugExtendDlg::CPlugExtendDlg(QStringView plug_path, const QStringList& filenames, QWidget* parent)
	:CPlugExtendDlg(parent)
{
	d_ptr->m_plug_path = plug_path.toString();
	d_ptr->m_filenames = filenames;
	OnInitDialog();
	InitSlot();
}

CPlugExtendDlg::~CPlugExtendDlg()
{
	if (d_ptr)
		delete d_ptr;
}

void CPlugExtendDlg::OnInitDialog()
{
	this->setWindowFlags(this->windowFlags() & (~Qt::WindowContextHelpButtonHint));
	this->setWindowTitle(tr("PlugExtend"));
	ui.treeWidget->setAlternatingRowColors(false);
	ui.treeWidget->setSelectionMode(QAbstractItemView::NoSelection);
	ui.treeWidget->setColumnCount(1);
	ui.treeWidget->setHeaderHidden(true);
	d_ptr->m_interface_icon.addPixmap(this->style()->standardPixmap(QStyle::SP_DirOpenIcon),QIcon::Normal,QIcon::On);
	d_ptr->m_interface_icon.addPixmap(this->style()->standardPixmap(QStyle::SP_DirClosedIcon), QIcon::Normal, QIcon::Off);
	d_ptr->m_feature_icon.addPixmap(this->style()->standardPixmap(QStyle::SP_FileIcon));
	
	FindPlugins(d_ptr->m_plug_path, d_ptr->m_filenames);
}

void CPlugExtendDlg::InitSlot()
{
	connect(ui.pushButton_ok, &QPushButton::clicked, this, &QDialog::close);
}

void CPlugExtendDlg::FindPlugins(QStringView plug_path, const QStringList& filenames)
{
	ui.label_content->setText(tr("Plugin Dialog following plugins""(looked in %1)").arg(plug_path));
	const QDir _dir(plug_path.toString());
	const auto _static_interfaces = QPluginLoader::staticInstances();
	for (const auto i : _static_interfaces)
		PopulateTreeWidget(i, tr("%1 (static plugins)").arg(i->metaObject()->className()));
	for (const auto& i : filenames)
	{
		QPluginLoader _loader(_dir.absoluteFilePath(i));
		QObject* _plugin{};
		if (_plugin = _loader.instance(), _plugin)
			PopulateTreeWidget(_plugin, i);
	}
}

void CPlugExtendDlg::PopulateTreeWidget(QObject* plugin, QStringView text)
{
	//plugin_item
	auto _first_item = new QTreeWidgetItem(ui.treeWidget);
	_first_item->setText(0,text.toString());
	_first_item->setExpanded(true);
	QFont _font{_first_item->font(0)};
	_font.setBold(true);
	_first_item->setFont(0,_font);

	if (plugin)
	{
		auto _iBrush = qobject_cast<BrushInterfaces*>(plugin);
		if (_iBrush)
			AddItems(_first_item, "BrushInterfaces", _iBrush->brushes());
		ShapeInterfaces* _iShape{};
		if (_iShape = qobject_cast<ShapeInterfaces*>(plugin), _iShape)
			AddItems(_first_item, "ShapeInterfaces", _iShape->shapes());
		FilterInterfaces* _iFilter{};
		if (_iFilter = qobject_cast<FilterInterfaces*>(plugin), _iFilter)
			AddItems(_first_item, "FilterInterfaces", _iFilter->filters());
	}
}

void CPlugExtendDlg::AddItems(QTreeWidgetItem* plug_item, const char* interface_name, const QStringList& features)
{
	auto _interface_item = new QTreeWidgetItem(plug_item);
	_interface_item->setText(0, interface_name);
	_interface_item->setIcon(0, d_ptr->m_interface_icon);
	for (const auto& i : features)
	{
		auto _feature_item = new QTreeWidgetItem(_interface_item);
		_feature_item->setText(0,i);
		_feature_item->setIcon(0,d_ptr->m_feature_icon);
	}
}

CPlugExtendDlgPrivate::CPlugExtendDlgPrivate(CPlugExtendDlg* parent)
	:q_ptr{parent}
{
}

CPlugExtendDlgPrivate::~CPlugExtendDlgPrivate()
{
}
